package eqp2;

// Eight Queens Puzzle
// by Ron Daenzer 
// place 8 queens on a chess board without conflict 
// features: user interactive event, visual board, undo place queen, visual help
// http://en.wikipedia.org/wiki/Eight_queens_puzzle 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class EQP2 extends JFrame {
    
    static boolean[] board = new boolean[64];   //chess board has 64 positions
    // 0,14,19,29,39,41,52,58 
    static int queens = 0;                      //number of queens
    static JFrame jf = new JFrame("Eight Queens Puzzle");
    static JButton[] jb = new JButton[64];

    ////////////place Queen at position on board
    static void placeQueen(int p, boolean b) {
        int r = p / 8;
        int c = p % 8;
        for (int h = r * 8; h < r * 8 + 8; h++) {
            board[h] = b;
        }
        for (int v = c; v < 64; v = v + 8) {
            board[v] = b;
        }
        //diagnal x
        int x1, x2;
        if (c > r) {
            x1 = c - r;
            x2 = p + (9 * (8 - c));
        } else {
            x1 = (r - c) * 8;
            x2 = p + (9 * (8 - r));
        }
        for (int x = x1; x < x2; x = x + 9) {
            board[x] = b;
        }
        //diagnal y
        int y1, y2;
        if (r + c > 7) {
            y1 = p - ((7 - c) * 7);
            y2 = p + (7 * (8 - r));
        } else {
            y1 = r + c;
            y2 = p + (8 * c);
        }
        for (int y = y1; y < y2; y = y + 7) {
            board[y] = b;
        }
    }

    //find queen and repair the board
    static void repairBoard() {
        for (int p = 0; p < 64; p++) {
            if (jb[p].getText().equals("Q")) {
                placeQueen(p, true);
            }
        }
    }

    //set Board Color
    static void setBoardColor() {
        for (int p = 0; p < 64; p++) {
            if (board[p] == true) {
                jb[p].setBackground(Color.yellow);
            } else {
                jb[p].setBackground(Color.WHITE);
            }
        }
    }

    //
    static void winner() {
        for (int p = 0; p < 64; p++) {
            if (jb[p].getText().equals("Q")) {
                jb[p].setBackground(Color.RED);
            }
        }
    }

    ///////////////////////////////////////////////////////////
    public static void main(String[] args) {
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setSize(500, 500);
        jf.setLayout(new GridLayout(8, 8));   //8x8 chess board
        //////////////////setup event listener
        ActionListener al = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                JButton b = (JButton) ae.getSource();
                String qp = b.getActionCommand();     //get position of click
                int p = Integer.parseInt(qp);     //position of mouse click
                /////////////////if safe then place queen
                if (!board[p]) {
                    queens++;
                    b.setText("Q");
                    //b.setBackground(Color.YELLOW);
                    placeQueen(p, true);
                } else if (b.getText().equals("Q")) {   //remove queen
                    queens--;
                    b.setText("");
                    //b.setBackground(Color.WHITE);
                    placeQueen(p, false);
                    repairBoard();
                    jf.setTitle("Eight Queens Puzzle");
                    jf.setSize(500, 500);
                }
                //set Board Color
                setBoardColor();

                //if 8 queens then you win
                if (queens >= 8) {
                    jf.setTitle("Eight Queens Puzzle - WINNER");
                    //jf.setSize(700, 700);
                    winner();
                }
            }
        };
        //initialize board
        for (int p = 0; p < 64; p++) {
            board[p] = false;
            jb[p] = new JButton();
            jb[p].setActionCommand(p + "");
            //jb[p].setText(p + "");
            jb[p].addActionListener(al);
            jb[p].setBackground(Color.WHITE);
            jf.getContentPane().add(jb[p]);
        }
        jf.setVisible(true);
    }
}
